package cn.bjxy.javaks.store_ms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.bjxy.javaks.store_ms.entity.LogEntity;
import cn.bjxy.javaks.store_ms.utils.JdbcUtil;

public class LogDaoImpl implements ILogDao{

	private Connection con;
	private PreparedStatement pStat;
	private ResultSet rs;
	
	@Override
	public List<LogEntity> findAll() {
		try {
			String sql = "SELECT * FROM log_info";
			con = JdbcUtil.getConnect();
			pStat = con.prepareStatement(sql);
			rs = pStat.executeQuery();
			
			ArrayList<LogEntity> logList = new ArrayList<LogEntity>();
			//遍历结果集
			while(rs.next()) {
				LogEntity log = new LogEntity();   //封装查询到的管理员信息
				log.setId(rs.getString("id"));
				log.setAdmin_name(rs.getString("admin_name"));
				log.setContent(rs.getString("content"));
				log.setOperate_obj(rs.getString("operate_obj"));
				log.setOperate_type(rs.getString("operate_type"));
				log.setOperate_obj_id(rs.getString("operate_obj_id"));
				log.setOperate_date(rs.getTimestamp("operate_date"));
				logList.add(log);
			}
			return logList;
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}finally {
			JdbcUtil.closeAll(con, pStat, rs);
		}
	}

	@Override
	public LogEntity findById(String id) {
		try {
			String sql = "SELECT * FROM log_info WHERE id=?";
			con = JdbcUtil.getConnect();
			pStat = con.prepareStatement(sql);
			pStat.setString(1, id);
			rs = pStat.executeQuery();
			
			LogEntity log = null;   //封装查询到的管理员信息
			//遍历结果集
			if(rs.next()) {
				log = new LogEntity();   //封装查询到的管理员信息
				log.setId(rs.getString("id"));
				log.setAdmin_name(rs.getString("admin_name"));
				log.setContent(rs.getString("content"));
				log.setOperate_obj(rs.getString("operate_obj"));
				log.setOperate_type(rs.getString("operate_type"));
				log.setOperate_obj_id(rs.getString("operate_obj_id"));
				log.setOperate_date(rs.getTimestamp("operate_date"));
			}
			return log;
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}finally {
			JdbcUtil.closeAll(con, pStat, rs);
		}
	}

	@Override
	public List<LogEntity> findByName(String name) {
		try {
			String sql = "SELECT * FROM log_info WHERE admin_name=?";
			con = JdbcUtil.getConnect();
			pStat = con.prepareStatement(sql);
			pStat.setString(1, name);
			rs = pStat.executeQuery();
			
			ArrayList<LogEntity> logList = new ArrayList<LogEntity>();
			//遍历结果集
			while(rs.next()) {
				LogEntity log = new LogEntity();   //封装查询到的管理员信息
				log.setId(rs.getString("id"));
				log.setAdmin_name(rs.getString("admin_name"));
				log.setContent(rs.getString("content"));
				log.setOperate_obj(rs.getString("operate_obj"));
				log.setOperate_type(rs.getString("operate_type"));
				log.setOperate_obj_id(rs.getString("operate_obj_id"));
				log.setOperate_date(rs.getTimestamp("operate_date"));
				logList.add(log);
			}
			return logList;
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}finally {
			JdbcUtil.closeAll(con, pStat, rs);
		}
	}

	@Override
	public int delete(String id) {
		try {
			String sql = "delete from log_info where id=?";
			con = JdbcUtil.getConnect();
			pStat = con.prepareStatement(sql);
			
			//填充数据
			pStat.setString(1,id);
			
			return pStat.executeUpdate();
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}finally {
			JdbcUtil.closeAll(con, pStat, rs);
		}
	}

	@Override
	public int add(LogEntity log) {
		try {
			String sql = "INSERT INTO log_info VALUES(?,?,?,?,?,?,?);";
			con = JdbcUtil.getConnect();
			pStat = con.prepareStatement(sql);
			
			//填充数据
			pStat.setString(1,log.getId());
			pStat.setString(2, log.getAdmin_name());
			pStat.setString(3, log.getContent());
			pStat.setString(4, log.getOperate_type());
			pStat.setString(5, log.getOperate_obj());
			pStat.setString(6, log.getOperate_obj_id());
			pStat.setTimestamp(7, new Timestamp(new Date().getTime()));
			return pStat.executeUpdate();
		}catch (SQLException e) {
			throw new RuntimeException(e);
		}finally {
			JdbcUtil.closeAll(con, pStat, rs);
		}
	}
}
